import { Injectable } from "@angular/core";
import { throwError, of } from "rxjs";
import { User } from "src/app/@shared/models/user";

const USERS = [];

@Injectable()
export class AuthService {
  constructor() { }

  login(account: string, password: string) {
    for (let i = 0; i < USERS.length; i++) {
      if (account === USERS[i].account && password === USERS[i].password) {
        let { userName, gender, phoneNumber, email } = USERS[i];
        let userInfo: User = { userName, gender, phoneNumber, email };
        return of(userInfo);
      }
    }
    return throwError(
      "Please make sure you have input correct account and password"
    );
  }

  logout() {
    localStorage.removeItem("id_token");
    localStorage.removeItem("expires_at");
    localStorage.removeItem("userinfo");
  }

  setSession(userInfo: User) {
    localStorage.setItem("id_token", "123456");
    localStorage.setItem("userinfo", JSON.stringify(userInfo));
    localStorage.setItem("expires_at", "120");
  }

  isUserLoggedIn() {
    if (localStorage.getItem("userinfo")) {
      return true;
    } else {
      return false;
    }
  }
}
